<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	 function __construct()
    {
        parent::__construct();
    }
	public function index()
	{
		$html=$this->init("用戶管理","jquery","table");
		$this->load->model('users_model');
		$this->load->model('role_model');
		$html["users"]=$this->users_model->get_user();
		$html["roles"]=$this->role_model->get_all();
		$this->load->view('user',$html);
	}
	
	public function add(){
		
		$html=$this->init("用戶添加","jquery","form");
		$this->load->model('department_model');
		$html["department"]=$this->department_model->get_all();
		$this->load->model('role_model');
		$html["role"]=$this->role_model->get_all();
		$this->load->view('user_add',$html);
	}

	public function add_submit()
	{
		$this->load->model('users_model');
		$data['user'] = $this->input->post('user');
		$data['password'] = MD5($this->input->post('password'));
		$data['department_id'] = $this->input->post('department_id');
		$data['role_id'] = $this->input->post('role_id');
		$data['name'] = $this->input->post('name');
		$data['phone'] = $this->input->post('phone');
		$data['email'] = $this->input->post('email');
		$return_result=$this->users_model->add_user($data);
		if($return_result>0){
			redirect("/user");
		}else{
			echo "插入失敗";
		}
		
	}
	public function edit($id=null)
	{
		$html=$this->init("用戶編輯","jquery","form");
		$this->load->model('users_model');
		$html["users"]=$this->users_model->get_user_info($id);
		$this->load->model('department_model');
		$html["department"]=$this->department_model->get_all();
		$this->load->model('role_model');
		$html["role"]=$this->role_model->get_all();
		$this->load->view('user_edit',$html);
	}
	public function edit_submit()
	{
		$this->load->model('users_model');
		$data['id']= $this->input->post('users_id');
		

		if($this->input->post('password')!=""){

			$data['password'] = $this->input->post('password');
			
			$this->users_model->update_user_password($data);
		}

		$data['name'] = $this->input->post('name');
		$data['email'] = $this->input->post('email');
		$data['phone'] = $this->input->post('phone');				
		$data['department_id'] = $this->input->post('department_id');
		$data['role_id'] = $this->input->post('role_id');
		$return_result=$this->users_model->update_user($data);
		//echo $this->db->last_query();;
		if($return_result){
			redirect("/user");
		}else{
			echo "編輯失敗";
		}
	
	}
	
	public function delete($id=null){
		$this->load->model('users_model');
		if($id!=null)
		{
			$data["id"]=$id;
			$return_result=$this->users_model->del_user($data);
			if($return_result){
				redirect("/user");
			}else{
				echo "刪除失敗";
			}
		}else{
			echo "刪除失敗";
		}	
	}

	public function get_user(){

		$this->load->model('users_model');

		$user = $this->input->post('user');		

		if( !empty($user) ){

			$result_user =$this->users_model->get_user_s_user($user);
			

			if($result_user->num_rows > 0){
				echo 'error';
			}else{
				echo 'ok';
			}


		}	
	}

	public function get_user_name(){

		$this->load->model('users_model');

		$name = $this->input->post('name');		

		if( $name){

			$result_name = $this->users_model->get_user_s_name($name);

			//print_r($result_name->result());

			if($result_name->num_rows > 0){
				echo 'error';
			}else{
				echo 'ok';
			}


		}	
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */